package chan.project.smartlawregularuser.controller.inner;


import chan.project.smartlawcommon.common.ErrorCode;
import chan.project.smartlawcommon.exception.BusinessException;
import chan.project.smartlawcommon.exception.ThrowUtils;
import chan.project.smartlawmodel.entity.CommonInfo;
import chan.project.smartlawservice.commonService.CommonInfoFeignClient;
import chan.project.smartlawregularuser.service.CommonInfoService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/common")
@Tag(name = "用户模块内部接口")
public class CommonInfoControllerInner implements CommonInfoFeignClient {

    @Resource
    private CommonInfoService commonInfoService;

    @Override
    @Operation(summary = "register")
    @PostMapping("/inner/register")
    public boolean commonRegister(@RequestBody CommonInfo commonInfo) {
        if(ObjectUtil.isEmpty(commonInfo)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LambdaQueryWrapper<CommonInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommonInfo::getUserId,commonInfo.getUserId());
        CommonInfo one = commonInfoService.getOne(queryWrapper);
        if(ObjectUtil.isNotEmpty(one)){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"该用户已有账号");
        }
        boolean save = commonInfoService.save(commonInfo);
        ThrowUtils.throwIf(!save,ErrorCode.OPERATION_ERROR);
        return save;
    }

    @Override
    @Operation(summary = "getCommonInfo")
    @GetMapping("/inner/getCommon")
    public CommonInfo getCommonInfo(@RequestParam("userId") Long userId) {
        LambdaQueryWrapper<CommonInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommonInfo::getUserId,userId);
        CommonInfo one = commonInfoService.getOne(queryWrapper);
        return one;
    }
}
